package com.ysd.lis.service.emr.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.emr.EmrProcessDto;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.emr.*;
import com.ysd.lis.mapper.emr.EmrProcessDetailMapper;
import com.ysd.lis.service.emr.EmrProcessDetailService;
import com.ysd.util.RedisUserManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-06-10
 */
@Service
public class EmrProcessDetailServiceImpl extends ServiceImpl<EmrProcessDetailMapper, EmrProcessDetail> implements EmrProcessDetailService {

    @Override
    public Result findEmrProcessDetailList(EmrProcessDetail emrProcessDetail) {
        MPJLambdaWrapper<EmrProcessDetail> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAs(EmrProcessDetail::getId, EmrProcessDto::getId);
        wrapper.selectAs(EmrProcessDetail::getDetatilStatus, EmrProcessDto::getDetatilStatus);
        wrapper.selectAs(EmrVersionRole::getRoleName, EmrProcessDto::getRoleName);
        wrapper.selectAs(EmrItem::getItemName, EmrProcessDto::getItemName);
        wrapper.selectAs(EmrItemStandardTask::getTaskName, EmrProcessDto::getTaskName);
        wrapper.selectAs(EmrItemStandardTask::getId, EmrProcessDto::getTaskId);
        wrapper.leftJoin(EmrItemStandardTask.class, p -> p.eq(EmrItemStandardTask::getId, EmrProcessDetail::getStandardTaskId).eq(EmrItemStandardTask::getDelFlag, 0));
        wrapper.leftJoin(EmrVersionRole.class, p -> p.eq(EmrVersionRole::getVersionCode, EmrItemStandardTask::getVersionCode).eq(EmrVersionRole::getRoleCode, EmrItemStandardTask::getRoleCode).eq(EmrVersionRole::getDelFlag, 0));
        wrapper.leftJoin(EmrItem.class, p -> p.eq(EmrItem::getVersionCode, EmrItemStandardTask::getVersionCode).eq(EmrItem::getRoleCode, EmrItemStandardTask::getRoleCode).eq(EmrItem::getItemCode, EmrItemStandardTask::getItemCode).eq(EmrItem::getDelFlag, 0));
        wrapper.eq(EmrProcessDetail::getDelFlag, 0);
        wrapper.eq(EmrProcessDetail::getProcessCode, emrProcessDetail.getProcessCode());
        List<EmrProcessDto> emrProcessDtos = this.selectJoinList(EmrProcessDto.class, wrapper);
        return Result.succ(1, "查询成功", emrProcessDtos);
    }

    @Override
    public Result findEmrProcessDetailListByUser(EmrProcessDetail emrProcessDetail) {
        SysUser sysUser = RedisUserManager.getUser();
        if (Objects.isNull(sysUser) || StringUtils.isBlank(sysUser.getUsername())) {
            return Result.fail("未找到当前用户信息");
        }
        MPJLambdaWrapper<EmrProcessDetail> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAs(EmrProcessDetail::getId, EmrProcessDto::getId);
        wrapper.selectAs(EmrProcessDetail::getDetatilStatus, EmrProcessDto::getDetatilStatus);
        wrapper.selectAs(EmrVersionRole::getRoleName, EmrProcessDto::getRoleName);
        wrapper.selectAs(EmrItem::getItemName, EmrProcessDto::getItemName);
        wrapper.selectAs(EmrItemStandardTask::getTaskName, EmrProcessDto::getTaskName);
        wrapper.selectAs(EmrItemStandardTask::getId, EmrProcessDto::getTaskId);
        wrapper.selectAs(EmrItemStandardTaskFile::getFileUrl, EmrProcessDto::getTemplateFileUrl);
        wrapper.selectAs(EmrProcessDetailFile::getFileUrl, EmrProcessDto::getDetailFileUrl);
        wrapper.leftJoin(EmrItemStandardTask.class, p -> p.eq(EmrItemStandardTask::getId, EmrProcessDetail::getStandardTaskId).eq(EmrItemStandardTask::getDelFlag, 0));
        wrapper.leftJoin(EmrVersionRole.class, p -> p.eq(EmrVersionRole::getVersionCode, EmrItemStandardTask::getVersionCode).eq(EmrVersionRole::getRoleCode, EmrItemStandardTask::getRoleCode).eq(EmrVersionRole::getDelFlag, 0));
        wrapper.leftJoin(EmrItem.class, p -> p.eq(EmrItem::getVersionCode, EmrItemStandardTask::getVersionCode).eq(EmrItem::getRoleCode, EmrItemStandardTask::getRoleCode).eq(EmrItem::getItemCode, EmrItemStandardTask::getItemCode).eq(EmrItem::getDelFlag, 0));
        wrapper.leftJoin(EmrProcessDetailUser.class, EmrProcessDetailUser::getProcessDetailId, EmrProcessDetail::getId);
        wrapper.leftJoin(EmrItemStandardTaskFile.class, EmrItemStandardTaskFile::getTaskId, EmrProcessDetail::getStandardTaskId);
        wrapper.leftJoin(EmrProcessDetailFile.class, EmrProcessDetailFile::getProcessDetailId, EmrProcessDetail::getId);
        wrapper.eq(EmrProcessDetailUser::getUserCode, sysUser.getUsername());
        wrapper.eq(EmrProcessDetail::getDelFlag, 0);
        wrapper.eq(EmrProcessDetail::getProcessCode, emrProcessDetail.getProcessCode());
        List<EmrProcessDto> emrProcessDtos = this.selectJoinList(EmrProcessDto.class, wrapper);
        return Result.succ(1, "查询成功", emrProcessDtos);
    }
}
